package sz.szsmk.citizencard.ui.newversion.activity

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.chenenyu.router.annotation.Route
import com.gyf.immersionbar.ktx.immersionBar
import kotlinx.android.synthetic.main.activity_wheretodetail.*
import sz.szsmk.citizencard.R
import sz.szsmk.citizencard.ui.newversion.activity.adapter.BusSegmentListAdapterNew
import sz.szsmk.citizencard.ui.newversion.bean.CollectionList
import sz.szsmk.citizencard.ui.newversion.bean.MapCollectionBean
import sz.szsmk.citizencard.ui.newversion.config.Constants
import sz.szsmk.citizencard.ui.newversion.utils.AMapUtil
import sz.szsmk.citizencard.util.PermissionUtil
import sz.szsmk.citizencard.util.SecureSharedPreferences
import sz.szsmk.citizencard.util.zxyh.ResourceStyleUtils


/**
 *  author : hyt
 *  date : 2020/9/4
 *  description :   路线详情
 */
@Route(Constants.WHERETODETAIL)
class WhereToDetailActivity : sz.szsmk.citizencard.ui.dzsmkavtivity.AppBaseActivity() {

    private lateinit var startStation: String
    private lateinit var endStation: String
    var collectionList: CollectionList? = null
    var mBusSegmentListAdapterNew: BusSegmentListAdapterNew? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wheretodetail)

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            PermissionUtil.checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE, getResources().getString(R.string.write_permission))
        }

        //沉浸
        immersionBar {
            reset()
            fitsSystemWindows(true)
            statusBarColor(R.color.color_2184FA)
            statusBarDarkFont(false)
        }
        //手动 空出 状态栏高度
//        var layoutParams = toolbar.layoutParams as ViewGroup.MarginLayoutParams
//        layoutParams.setMargins(0, ImmersionBar.getStatusBarHeight(this), 0, 0)
//        toolbar.layoutParams = layoutParams

        back.setOnClickListener { finish() }


        startStation = intent?.extras!!.getString("startStation") as String
        endStation = intent?.extras!!.getString("endStation") as String

/*
 4号口
 183.0156
轨道交通2号线(骑河--桑田岛) 05:40-23:06
passStation=苏州火车站
passStation=山塘街
passStation=石路
arrvivalStation=广济南路0平河路0
 3号口
 177.0151
轨道交通1号线(木渎--钟南街) -
passStation=养育巷
passStation=乐桥
passStation=临顿路
arrvivalStation=相门0广济南路0
 961.0823
 */
        bus_segment_list.layoutManager = LinearLayoutManager(this)
        bus_segment_list.adapter = mBusSegmentListAdapterNew


        var allStepNum = 0

        collectionList = SecureSharedPreferences.getSerObject("CollectionList") as CollectionList?

        if (collectionList == null) {
            SecureSharedPreferences.putSerObject("CollectionList", CollectionList())
            collectionList = SecureSharedPreferences.getSerObject("CollectionList") as CollectionList?
        }

        var beanTemp = MapCollectionBean()
        beanTemp.startStation = startStation
        beanTemp.endStation = endStation

        if (collectionList?.list?.contains(beanTemp)!!) {
            btn_collection.text = "已收藏"
        } else {
            btn_collection.text = "收藏"
        }
        //用多长时间 走多少路

        fl_busLine.getChildAt(fl_busLine.childCount - 1)
                .findViewById<ImageView>(R.id.iv_next).visibility = View.GONE
        btn_collection.setOnClickListener {
            if (collectionList?.list?.contains(beanTemp)!!) {
                //找到 取消收藏
                collectionList?.list?.remove(beanTemp)
                btn_collection.text = "收藏"
            } else {
                collectionList?.list?.add(beanTemp)
                btn_collection.text = "已收藏"
            }

            SecureSharedPreferences.putSerObject("CollectionList", collectionList)
        }

    }

    override fun getTopTitle() = null
    fun getTextView(text: String): View {
        var view: View = LayoutInflater.from(this).inflate(
                R.layout.view_whereto_textview, null, false)
        var textView = view.findViewById(R.id.tv_text) as TextView

        textView.text = text
        ResourceStyleUtils.setMetrolLineBack(this, textView);
        return view
    }


}
